<!DOCTYPE html>
<html>

<head>
    <title>登录</title>
    <!-- 导入Google Fonts字体 -->
    <link href="https://fonts.googleapis.com/css?family=Montserrat:400,700" rel="stylesheet">
    <style>
        /* 应用于整个网页 */
        body {
            margin: 0;
            padding: 0;
            font-family: 'Montserrat', sans-serif;
            background-color: rgb(221, 225, 225);
        }

        /* 应用于容器 */
        .container {
            margin: 0 auto;
            width: 450px;
            padding: 20px;
            background-color: #fff;
            box-shadow: 0px 0px 20px rgba(0, 0, 0, 0.1);
        }

        h1 {
            text-align: center;
            font-size: 32px;
            margin-bottom: 30px;
        }

        form {
            display: flex;
            flex-direction: column;
        }

        label {
            font-size: 16px;
            margin-top: 10px;
        }

        input {
            border-radius: 5px;
            border: 2px solid #cccccc;
            padding: 10px;
            margin-bottom: 20px;
            font-size: 16px;
        }

        input:focus {
            outline: none;
            border-color: #5E73BD;
            box-shadow: 0px 0px 5px rgba(94, 115, 189, 0.5);
        }

        button {
            background-color: #5E73BD;
            color: #fff;
            border: none;
            border-radius: 5px;
            padding: 10px;
            margin-top: 20px;
            cursor: pointer;
            font-size: 18px;
            transition-duration: 0.4s;
        }

        button:hover {
            background-color: #32408f;
        }

        .register-link {
            font-size: 14px;
            margin-top: 20px;
            text-align: center;
        }

        .register-link a {
            color: #5E73BD;
            text-decoration: none;
        }

        .register-link a:hover {
            text-decoration: underline;
        }

        img {
            max-width: 200px;
            max-height: 200px;
        }
    </style>
</head>

<body>
    <div class="container">
        <!-- TODO -->
        <div style="text-align: center;">
            <img src="/logo.png" alt="只因华太美">
        </div>
        <form id="login-form">
            <label for="username">用户名</label>
            <input type="text" id="username" name="username" required>

            <label for="password">密码</label>
            <input type="password" id="password" name="password" minlength="8" required>

            <button type="submit">登录</button>

            <p class="register-link">还没有注册？<a href="/register">去注册</a></p>
        </form>
    </div>
    <!-- 引入 jQuery 库 -->
    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
    <script>
        $(document).ready(function () {
            $('#login-form').submit(function (e) {
                e.preventDefault();
                var form_data = {
                    username: $('#username').val(),
                    password: $('#password').val()
                };

                $.ajax({
                    type: "POST",
                    url: "/login",
                    data: JSON.stringify(form_data),
                    contentType: "application/json",
                    dataType: "json",
                    success: function (data) {
                        if (data.status === 'success') {
                            window.location.href = "/index_1.html";
                        } else {
                            alert(data.err_information);
                        }
                    },
                    error: function (error) {
                        console.log(error);
                        alert('登录失败，请稍后再试！');
                    }
                });
            });
        });
    </script>
</body>

</html>